import { observable, action, configure, computed, runInAction} from "mobx"

configure({enforceActions: 'observed'});

class AppleStore {
    @observable currentAppleList = [];
    @observable eatedAppleList = [];
    @observable idPicking = false;
    @observable currentAppleIndex = 1;

    @computed get getCurrentAppleWight () {
        let total = 0
        this.currentAppleList.map(apple => {
            total += apple.weight
        })

        return total
    }
    @computed get getEatedAppleWight () {
        let total = 0
        this.eatedAppleList.map(apple => {
            total += apple.weight
        })
        return total
    }
    @action.bound pickApple() {
        this.idPicking = true;
        this.currentAppleList.push({
            index:this.currentAppleIndex,
            weight: 200 + Math.floor(Math.random() * 10) 
        });
        this.currentAppleIndex = this.currentAppleIndex + 1;
        setTimeout(() => {
            runInAction( () => {
                this.idPicking = false;
            })
        }, 500)
        
    }
    @action.bound eatApple (index) {
        let currentEatedApple; 
        this.currentAppleList = this.currentAppleList.filter(apple => {
            if (apple.index === index) {
                currentEatedApple = apple;
            }
            return apple.index !== index
        })
        this.eatedAppleList.push(currentEatedApple)
    }
}

const appleStore = new AppleStore()

export default appleStore